﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.MQBase
{
    /// <summary>
    /// MQ消息队列基类
    /// </summary>
    public abstract class MQMessageQueueAbs : IMQMessageQueue
    {
        /// <summary>
        /// MQ连接信息
        /// </summary>
        protected readonly MQBrokerInfo _brokerInfo;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="brokerInfo">MQ连接信息</param>
        public MQMessageQueueAbs(MQBrokerInfo brokerInfo)
        {
            this._brokerInfo = brokerInfo;
        }

        /// <summary>
        /// 获取BrokerInfo
        /// </summary>
        public MQBrokerInfo BrokerInfo
        {
            get { return this._brokerInfo; }
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="message">消息</param>
        /// <param name="strategy">发布策略</param>
        public void Push(string topic, IMQMessage message, IMessagePublishStrategy strategy)
        {
            this.PrimitivePush(topic, message, strategy);
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="message">消息</param>
        /// <param name="strategy">发布策略</param>
        protected abstract void PrimitivePush(string topic, IMQMessage message, IMessagePublishStrategy strategy);

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="subscribInfo">订阅对象</param>
        public void Subscribe(IMQSubscribInfo subscribInfo)
        {
            this.PrimitiveSubscribe(subscribInfo);
        }

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="subscribInfo">订阅对象</param>
        protected abstract void PrimitiveSubscribe(IMQSubscribInfo subscribInfo);

        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="subscribInfo">订阅对象</param>
        public void UnSubscribe(IMQSubscribInfo subscribInfo)
        {
            this.PrimitiveUnSubscribe(subscribInfo);
        }

        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="subscribInfo">订阅对象</param>
        protected abstract void PrimitiveUnSubscribe(IMQSubscribInfo subscribInfo);

        /// <summary>
        /// 清空订阅
        /// </summary>
        public void Clear()
        {
            this.PrimitiveClear();
        }

        /// <summary>
        /// 清空订阅
        /// </summary>
        protected abstract void PrimitiveClear();


        /// <summary>
        /// 对象释放标识[true:已释放;fasle:未已释放]
        /// </summary>
        protected bool _disposed = false;
        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            if (this._disposed)
            {
                return;
            }
            this._disposed = true;

            try
            {
                this.Dispose(true);
            }
            catch (Exception ex)
            {
                ZLoger.Error(ex);
            }
        }

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing">flag</param>
        protected virtual void Dispose(bool disposing)
        {

        }
    }
}
